<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px"
      style="display: flex; align-items: center; flex-wrap: nowrap; gap: 15px; padding: 10px 0; white-space: nowrap; margin-left: 20px;"
      >
      <el-form-item label="设备类型编码" prop="machineryCode" style="margin-bottom: 5;">
        <el-input
          v-model="queryParams.machineryCode"
          placeholder="请输入设备类型编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型名称" prop="machineryName" style="margin-bottom: 5;">
        <el-input
          v-model="queryParams.machineryName"
          placeholder="请输入设备类型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"  v-if="props.fromWhere !== 'homeindex'">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['machinery:machinery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="props.fromWhere !== 'homeindex'">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['machinery:machinery:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="props.fromWhere !== 'homeindex'">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['machinery:machinery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['machinery:machinery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="machineryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型ID" align="center" prop="machineryId" />
      <el-table-column label="设备类型编码" align="center" prop="machineryCode" />
      <el-table-column label="设备类型名称" align="center" prop="machineryName" />
      <el-table-column label="品牌" align="center" prop="machineryBrand" />
      <el-table-column label="规格型号" align="center" prop="machinerySpec" />
      <el-table-column label="所属车间编码" align="center" prop="workshopCode" />
      <el-table-column label="所属车间名称" align="center" prop="workshopName" />
      <el-table-column label="设备状态" align="center" prop="status" />
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="props.fromWhere !== 'homeindex'">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['machinery:machinery:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['machinery:machinery:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备台帐对话框 -->
    <el-dialog :title="title" v-model="open" width="1300px" append-to-body>
  <el-form ref="machineryRef" :model="form" :rules="rules" label-width="80px">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="设备名称" prop="machineryName">
          <el-input v-model="form.machineryName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="品牌" prop="machineryBrand">
          <el-input v-model="form.machineryBrand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="设备分类" prop="machineryType">
          <el-select v-model="form.machineryType" placeholder="请选择设备分类">
            <el-option label="工具类" value="工具类"></el-option>
            <!-- 可根据需要添加更多选项 -->
          </el-select>
        </el-form-item>
        <el-form-item label="所属车间" prop="workshop">
          <el-select v-model="form.workshop" placeholder="请选择所属车间">
            <el-option label="1车间" value="1车间"></el-option>
            <!-- 可根据需要添加更多选项 -->
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备状态" prop="machineryStatus">
          <el-select v-model="form.machineryStatus" placeholder="请选择设备状态">
            <el-option label="停机" value="停机"></el-option>
            <!-- 可根据需要添加更多选项 -->
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="machinerySpec">
          <el-input v-model="form.machinerySpec" placeholder="请输入规格型号" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
    </el-form-item>
    <el-tabs v-model="activeTab" v-if="currentMachineryId">
      <el-tab-pane label="点检记录" name="inspection">
      <el-row style="margin-bottom: 10px;">
    <!-- <el-button type="primary" plain icon="Plus">新 增</el-button>
    <el-button type="danger" plain icon="Delete" style="margin-left: 10px;">删 除</el-button> -->
    <!-- <el-button type="primary" style="margin-left: auto;" >搜 索</el-button>
    <el-button type="primary" style="margin-left: 5px;" >刷 新</el-button> -->
      </el-row>  
        <el-table :data="inspectionRecords" v-loading="recordLoading" style="width: 100%;">
          <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column label="计划编码" align="center" prop="planCode" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="计划类型" align="center" prop="planType" />
      <el-table-column label="设备ID" align="center" prop="machineryId" />
      <el-table-column label="设备编码" align="center" prop="machineryCode" />
      <el-table-column label="设备名称" align="center" prop="machineryName" />
      <el-table-column label="品牌" align="center" prop="machineryBrand" />
      <el-table-column label="规格型号" align="center" prop="machinerySpec" />
      <el-table-column label="点检时间" align="center" prop="checkTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
          <!-- <el-table-column label="操作"> -->
          <!-- <template #default>
        <el-button type="text" >编 辑</el-button>
      </template> -->
    <!-- </el-table-column>     -->
        </el-table>
     <!-- 1. 点检记录分页（绑定点检专属参数） -->
        <el-pagination
          @size-change="(size) => { inspectionPageSize.value = size; getList1() }"
          @current-change="(page) => { inspectionPageNum.value = page; getList1() }"
          :current-page="inspectionPageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="inspectionPageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="inspectionTotal"
        />
      </el-tab-pane>
      <el-tab-pane label="保养记录" name="maintenance">
      <el-row style="margin-bottom: 10px;">
    <!-- <el-button type="primary" plain icon="Plus">新 增</el-button>
    <el-button type="danger" plain icon="Delete" style="margin-left: 10px;">删 除</el-button> -->
    <!-- <el-button type="primary" style="margin-left: auto;" >搜 索</el-button>
    <el-button type="primary" style="margin-left: 5px;" >刷 新</el-button> -->
      </el-row>   
        <el-table :data="maintenanceRecords" v-loading="recordLoading" style="width: 100%;">
          <!-- <el-table-column type="selection" width="55" /> -->
           <el-table-column label="计划ID" align="center" prop="planId" />
      <el-table-column label="计划编码" align="center" prop="planCode" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="计划类型" align="center" prop="planType" />
      <el-table-column label="设备ID" align="center" prop="machineryId" />
      <el-table-column label="设备编码" align="center" prop="machineryCode" />
      <el-table-column label="设备名称" align="center" prop="machineryName" />
      <el-table-column label="品牌" align="center" prop="machineryBrand" />
      <el-table-column label="规格型号" align="center" prop="machinerySpec" />
      <el-table-column label="保养时间" align="center" prop="maintenTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.maintenTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
          <!-- <el-table-column label="操作"> -->
          <!-- <template #default>
        <el-button type="text" >编 辑</el-button>
      </template> -->
    <!-- </el-table-column> -->
        </el-table>
    <!-- 2. 保养记录分页（绑定保养专属参数） -->
      <el-pagination
        @size-change="(size) => { maintenancePageSize.value = size; getList2() }"
        @current-change="(page) => { maintenancePageNum.value = page; getList2() }"
        :current-page="maintenancePageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="maintenancePageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="maintenanceTotal"
      />
      </el-tab-pane>
      <el-tab-pane label="维修记录" name="repair">
      <el-row style="margin-bottom: 10px;">
    <!-- <el-button type="primary" plain icon="Plus">新 增</el-button>
    <el-button type="danger" plain icon="Delete" style="margin-left: 10px;" >删 除</el-button> -->
    <!-- <el-button type="primary" style="margin-left: auto;" >搜 索</el-button>
    <el-button type="primary" style="margin-left: 5px;" >刷 新</el-button> -->
      </el-row>  
        <el-table :data="repairRecords" v-loading="recordLoading" style="width: 100%;">
          <!-- <el-table-column type="selection" width="55" /> -->
           <!-- <el-table-column label="维修单ID" align="center" prop="repairId" /> -->
      <el-table-column label="维修单编号" align="center" prop="repairCode" />
      <el-table-column label="维修单名称" align="center" prop="repairName" />
      <el-table-column label="设备编码" align="center" prop="machineryCode" />
      <el-table-column label="设备名称" align="center" prop="machineryName" />
      <el-table-column label="报修日期" align="center" prop="requireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.requireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修完成日期" align="center" prop="finishDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="验收日期" align="center" prop="confirmDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修结果" align="center" prop="repairResult" />
      <el-table-column label="维修人员" align="center" prop="acceptedBy" />
      <el-table-column label="验收人员" align="center" prop="confirmBy" />
      <el-table-column label="单据状态" align="center" prop="status" />
      
        <!-- 核心：详情按钮传递当前行的repairId和repairCode -->
      <el-table-column label="详情">
        <template #default="scope">
          <el-button 
            type="primary" 
            size="small" 
            @click="handleRepairLineDetail(scope.row.repairId, scope.row.repairCode)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>  
    </el-table>
   <!-- 3. 维修记录分页（绑定维修专属参数） -->
      <el-pagination
        @size-change="(size) => { repairPageSize.value = size; getList3() }"
        @current-change="(page) => { repairPageNum.value = page; getList3() }"
        :current-page="repairPageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="repairPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="repairTotal"
      />
      </el-tab-pane>
    </el-tabs>
  </el-form>
  <template #footer>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </template>
</el-dialog>
  <el-dialog
      :title="`维修单【${currentRepairCode}】- 维修单行详情`"
      v-model="repairLineDialogVisible"
      width="1200px"
      append-to-body
      @close="resetRepairLineData" 
    >
      <!-- 维修单行搜索栏（可选，支持二次筛选） -->
      <el-form :model="repairLineQuery" ref="repairLineQueryRef" :inline="true" label-width="80px" class="mb8">
        <el-form-item label="项目名称" prop="subjectName">
          <el-input
            v-model="repairLineQuery.subjectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter="getRepairLineList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getRepairLineList">搜索</el-button>
          <el-button icon="Refresh" @click="resetRepairLineQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 维修单行表格（直接渲染接口数据） -->
      <el-table 
        v-loading="repairLineLoading" 
        :data="repairLineList" 
        border
        style="width: 100%;"
      >
         <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="行ID" align="center" prop="lineId" />
      <el-table-column label="维修单ID" align="center" prop="repairId" />
      <el-table-column label="项目ID" align="center" prop="subjectId" />
      <el-table-column label="项目编码" align="center" prop="subjectCode" />
      <el-table-column label="项目名称" align="center" prop="subjectName" />
      <el-table-column label="项目类型" align="center" prop="subjectType" />
      <el-table-column label="项目内容" align="center" prop="subjectContent" />
      <el-table-column label="标准" align="center" prop="subjectStandard" />
      <el-table-column label="故障描述" align="center" prop="malfunction" />
      <el-table-column label="故障描述资源" align="center" prop="malfunctionUrl" />
      <el-table-column label="维修情况" align="center" prop="repairDes" />
      <el-table-column label="备注" align="center" prop="remark" />
      </el-table>

      <!-- 维修单行分页 -->
      <el-pagination
        v-show="repairLineTotal > 0"
        :total="repairLineTotal"
        v-model:page="repairLineQuery.pageNum"
        v-model:limit="repairLineQuery.pageSize"
        @pagination="getRepairLineList"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 30, 40]"
      />

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="repairLineDialogVisible = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Machinery">
import { listMachinery, getMachinery, delMachinery, addMachinery, updateMachinery } from "@/api/machinery/machinery"
import { ref, reactive, toRefs, getCurrentInstance, watch } from "vue" // 确保导入必要的Vue API（若原有代码未导入则补充）
import { listRecord } from "@/api/record/record"
import { listMaintenRecord } from "@/api/MaintenRecord/MaintenRecord"
import { listMachineRepair } from "@/api/ls1/machineRepair"
import { listRepairLine } from "@/api/repairLine/repairLine"
import { number } from "echarts"
const { proxy } = getCurrentInstance()

const machineryList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const currentMachineryId = ref(null) // 初始为null，代表未选中设备
// 点检记录分页
const inspectionTotal = ref(0)
const inspectionPageNum = ref(1)
const inspectionPageSize = ref(10)

// 保养记录分页
const maintenanceTotal = ref(0)
const maintenancePageNum = ref(1)
const maintenancePageSize = ref(10)
// 补充：记录表格的加载状态（独立于设备列表的loading）
const recordLoading = ref(false)

// 维修记录分页（与点检/保养保持一致，独立定义）
const repairTotal = ref(0)
const repairPageNum = ref(1)
const repairPageSize = ref(10)
// -------------------------- 新增：三个标签页模拟数据 --------------------------
// 1. 点检记录模拟数据（含编码、名称、内容、时间、频率、状态）
const inspectionRecords = ref([
])
// 核心新增：维修单行相关变量（直接在父组件内定义）
const repairLineDialogVisible = ref(false)  // 维修单行弹窗显示状态
const currentRepairId = ref(null)          // 当前选中的维修单ID（传递给接口）
const currentRepairCode = ref("")          // 当前维修单编号（用于标题）
const repairLineList = ref([])             // 维修单行数据列表
const repairLineTotal = ref(0)             // 维修单行总数
const repairLineLoading = ref(false)       // 维修单行加载状态
// 维修单行查询参数（含分页+筛选）
const repairLineQuery = reactive({
  pageNum: 1,
  pageSize: 10,
  repairId: null,       // 核心：通过维修单ID筛选
  subjectName: null     // 可选：按项目名称二次筛选
})

/** 查询设备点检记录列表 */

// 2. 保养记录模拟数据（含编码、名称、时间、频率、状态）
const maintenanceRecords = ref([

])
// 3. 维修记录模拟数据（含编码、名称、时间、故障类型、状态）
const repairRecords = ref([
])


// 标签页默认激活状态（默认显示"点检记录"）
const activeTab = ref('inspection')
// -------------------------- 新增结束 --------------------------

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    machineryCode: null,
    machineryName: null,
  },
  rules: {
    machineryCode: [
      { required: true, message: "设备类型编码不能为空", trigger: "blur" }
    ],
    machineryName: [
      { required: true, message: "设备类型名称不能为空", trigger: "blur" }
    ],
    machinerySpec: [
      { required: true, message: "规格型号不能为空", trigger: "blur" }
    ],
    machineryTypeId: [
      { required: true, message: "设备类型ID不能为空", trigger: "blur" }
    ],
    machineryTypeName: [
      { required: true, message: "设备类型名称不能为空", trigger: "blur" }
    ],
    workshopCode: [
      { required: true, message: "所属车间编码不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "设备状态不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)
//父子传值相关
const props = defineProps({
  fromWhere:{
    type:[Number,String],
    rrequired: false, // 独立页面时可不传
    default:null
  }
})

watch(()=>{
  ()=>props.fromWhere;
  (newVal) => {
    console.log("父组件传递的工序ID：", newVal); // 切换工序时，日志应同步变化
    if (newVal) { // 新增isMounted判断
      getList();
    }
  },
   { immediate: true }
})
/** 1. 查询当前设备的点检记录列表（关联点检分页参数） */
function getList1() {
  recordLoading.value = true
  // 携带：设备ID + 点检分页参数
  const params = { 
    pageNum: inspectionPageNum.value, // 点检专属页码
    pageSize: inspectionPageSize.value, // 点检专属页大小
    machineryId: currentMachineryId.value // 核心：筛选当前设备
  }
  listRecord(params).then(response => {
    inspectionRecords.value = response.rows
    inspectionTotal.value = response.total // 点检专属总数
    recordLoading.value = false
  }).catch(() => {
    recordLoading.value = false // 错误时也要关闭加载
  })
}

/** 2. 查询当前设备的保养记录列表（关联保养分页参数，修复语法错误） */
function getList2() {
  recordLoading.value = true
  // 携带：设备ID + 保养分页参数
  const params = { 
    pageNum: maintenancePageNum.value, // 保养专属页码
    pageSize: maintenancePageSize.value, // 保养专属页大小
    machineryId: currentMachineryId.value // 核心：筛选当前设备
  }
  // 修复：原代码多写了 .value（params本身是普通对象，不是ref）
  listMaintenRecord(params).then(response => {
    maintenanceRecords.value = response.rows
    maintenanceTotal.value = response.total // 保养专属总数
    recordLoading.value = false
  }).catch(() => {
    recordLoading.value = false // 错误时也要关闭加载
  })
}

/** 3. 维修记录查询函数（预留，与点检/保养逻辑一致） */
function getList3() {
  recordLoading.value = true
  // 后续对接后端时，替换为真实接口，逻辑同点检/保养
  const params = { 
    pageNum: repairPageNum.value,
    pageSize: repairPageSize.value,
    machineryId: currentMachineryId.value
  }
  // 模拟接口延迟（可删除，替换为真实接口调用）
  listMachineRepair(params).then(response =>{
    repairRecords.value=response.rows
    repairTotal.value = response.total
    recordLoading.value = false
  }).catch(()=>{
    recordLoading.value = false
  })
}

/** 查询设备台帐列表 */
function getList() {
  loading.value = true
  listMachinery(queryParams.value).then(response => {
    machineryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    machineryId: null,
    machineryCode: null,
    machineryName: null,
    machineryBrand: null,
    machinerySpec: null,
    machineryTypeId: null,
    machineryTypeCode: null,
    machineryTypeName: null,
    workshopId: null,
    workshopCode: null,
    workshopName: null,
    status: null,
    remark: null,
    attr1: null,
    attr2: null,
    attr3: null,
    attr4: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("machineryRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.machineryId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  currentMachineryId.value = null // 新增时重置为null（触发标签页隐藏）
  // 清空记录数据
  inspectionRecords.value = []
  maintenanceRecords.value = []
  repairRecords.value = []
  // 重置分页参数
  inspectionPageNum.value = 1
  maintenancePageNum.value = 1
  repairPageNum.value = 1
  open.value = true
  title.value = "添加设备台帐"
}

/** 修改按钮操作（打开设备详情） */
function handleUpdate(row) {
  reset()
  // 1. 获取当前设备的ID（row是选中的设备行数据，含machineryId）
  const _machineryId = row.machineryId || ids.value
  currentMachineryId.value = _machineryId // 绑定到当前设备ID
  
  // 2. 加载设备基本信息
  getMachinery(_machineryId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备台帐"
    
    // 3. 加载当前设备的点检+保养记录（关键：此时已绑定machineryId）
    // 重置分页参数，避免沿用之前的页码
    queryParams.value.pageNum = 1
    getList1() // 查询当前设备的点检记录
    getList2() // 查询当前设备的保养记录
    getList3()
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["machineryRef"].validate(valid => {
    if (valid) {
      if (form.value.machineryId != null) {
        updateMachinery(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMachinery(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _machineryIds = row.machineryId || ids.value
  proxy.$modal.confirm('是否确认删除设备台帐编号为"' + _machineryIds + '"的数据项？').then(function() {
    return delMachinery(_machineryIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('machinery/machinery/export', {
    ...queryParams.value
  }, `machinery_${new Date().getTime()}.xlsx`)
}
// 核心新增：维修单行相关方法
/** 1. 点击维修记录详情，打开弹窗并加载数据 */
function handleRepairLineDetail(repairId, repairCode) {
  currentRepairId.value = repairId  // 传递维修单ID
  currentRepairCode.value = repairCode  // 传递维修单编号（标题用）
  repairLineQuery.repairId = repairId  // 绑定查询参数
  repairLineQuery.pageNum = 1  // 重置页码
  getRepairLineList()  // 触发接口请求
  repairLineDialogVisible.value = true  // 打开弹窗
}

/** 2. 调用维修单行接口，加载数据 */
function getRepairLineList() {
  repairLineLoading.value = true
  // 接口参数：分页+维修单ID+筛选条件
  const params = { ...repairLineQuery }
  listRepairLine(params).then(response => {
    repairLineList.value = response.rows  // 赋值表格数据
    repairLineTotal.value = response.total  // 赋值总数
    repairLineLoading.value = false
  }).catch(error => {
    console.error("加载维修单行失败：", error)
    repairLineLoading.value = false
  })
}

/** 3. 重置维修单行查询条件 */
function resetRepairLineQuery() {
  proxy.resetForm("repairLineQueryRef")
  repairLineQuery.repairId = currentRepairId.value  // 保留维修单ID
  repairLineQuery.pageNum = 1
  getRepairLineList()
}

/** 4. 关闭弹窗时清空维修单行数据 */
function resetRepairLineData() {
  repairLineList.value = []
  repairLineTotal.value = 0
  repairLineQuery.subjectName = null
  proxy.resetForm("repairLineQueryRef")
}


getList()
</script>
